package com.ys.mb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ys.utils.base.j2se.DateUtil;
import com.ys.utils.base.j2se.Logger;
import com.ys.utils.mobao.Mobo360Config;
import com.ys.utils.mobao.Mobo360Merchant;
import com.ys.utils.mobao.Mobo360SignUtil;
import com.ys.utils.spring.config.SystemError;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: Mbtest
 * @Date: 2022/9/15 14:11
 * @Description:
 * @Version: 1.0
 */
public class Mbtest {
    @Test
    public void a(){
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("apiName", "QUERY_CARD_BIN");
        paramsMap.put("apiVersion", "1.0.0.0");
        paramsMap.put("custNo", "199950003582");
        paramsMap.put("spCardNo", "6226221102826515");
        String sourceData = null;
        try {
            sourceData = Mobo360SignUtil.coverMap2String(paramsMap);
            Mobo360SignUtil.initRAS("e:\\\\mb-private.pfx","e:\\\\mb-public.cer","121314");
            String signMsg = Mobo360SignUtil.signData(sourceData);
            paramsMap.put("signMsg", signMsg);

            String responseMsg = Mobo360Merchant.transact(Mobo360SignUtil.coverMap2String(paramsMap), "https://trade2.mobaopay.com/cashier/api/gateway.cgi");
            JSONObject jsonObject = JSON.parseObject(responseMsg);
            if(!jsonObject.getString("retMsg").equals("success") && !jsonObject.getString("retMsg").equals("成功")){
                Logger.error(responseMsg);
                SystemError.wrapBs("400",jsonObject.getString("retMsg"));
            }
//            System.err.println(responseMsg);
            responseMsg = jsonObject.getString("retData");
            System.err.println(JSON.parseObject(responseMsg).getInnerMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void alipay(){
        try{
            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("apiName","ALIPAY");
            paramsMap.put("custNo", Mobo360Config._merchantId);
            paramsMap.put("merchTradeNo", "20220831953298260987914");
            paramsMap.put("tradeAmt", "0.01");
            paramsMap.put("goodsName","测试");
            paramsMap.put("goodsDesc","测试");
            paramsMap.put("tradeTime", DateUtil.getDate(new Date(),"yyyyMMddHHmmss"));
            paramsMap.put("merchUrl","https://interface.ys168.vip/onlinePay/recharge/send");
            paramsMap.put("payEnvType",1);
            paramsMap.put("payType",172);
            paramsMap.put("merchParam","");
            paramsMap.put("tradeSummer","测试");
            paramsMap.put("product",170);
            paramsMap.put("terminalType",1);
            paramsMap.put("terminalId","123.146.199.108");
            String sourceData = Mobo360SignUtil.coverMap2String(paramsMap);
            Mobo360SignUtil.initRAS("e:\\\\mb-private.pfx","e:\\\\mb-public.cer","121314");
            String signMsg = Mobo360SignUtil.signData(sourceData);
            paramsMap.put("signMsg",signMsg);
            String responseMsg = Mobo360Merchant.transact(Mobo360SignUtil.coverMap2String(paramsMap), "https://trade2.mobaopay.com/cashier/api/gateway.cgi");
//            System.err.println(responseMsg);
            JSONObject jsonObject = JSON.parseObject(responseMsg);
            if(!jsonObject.getString("retMsg").equals("success") && !jsonObject.getString("retMsg").equals("成功")){
                Logger.error(responseMsg);
                SystemError.wrapBs("400","发起支付失败");
            }
            responseMsg = jsonObject.getString("retData");
//            return new String(new Base64().decode(JSON.parseObject(responseMsg).getString("codeUrl")), "UTF-8");
            System.err.println(JSON.parseObject(responseMsg).getInnerMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
